package com.jiangyg.mall.ware.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyg.mall.core.exception.BusinessException;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import com.jiangyg.mall.ware.constant.ColumnConstant;
import com.jiangyg.mall.ware.dao.WareInfoDao;
import com.jiangyg.mall.ware.dao.WareSkuDao;
import com.jiangyg.mall.ware.dto.WareInfoDTO;
import com.jiangyg.mall.ware.entity.WareInfo;
import com.jiangyg.mall.ware.entity.WareSku;
import com.jiangyg.mall.ware.vo.WareInfoVO;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WareInfoServiceImpl extends BaseServiceImpl<WareInfoDao, WareInfo> implements WareInfoService {

    private final WareSkuDao wareSkuDao;

    @Autowired
    public WareInfoServiceImpl(WareSkuDao wareSkuDao, WareInfoDao wareInfoDao) {
        super(wareInfoDao);
        this.wareSkuDao = wareSkuDao;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Pagination<WareInfoVO> selectPagination(Pagination<WareInfoVO> pagination) throws Exception {
        // 高级查询条件组装
        final QueryWrapper<WareInfo> wrapper = this.buildQueryWrapper(pagination, WareInfo.class);
        // 普通条件拼接
        final String condition = MapUtils.getString(pagination.toSearchMap(), "condition");
        if (StringUtils.isNotBlank(condition)) {
            wrapper.or().eq(ColumnConstant.ID, condition)
                    .or().like(ColumnConstant.NAME, condition)
                    .or().like(ColumnConstant.ADDRESS, condition)
                    .or().like(ColumnConstant.AREACODE, condition);
        }
        // 执行查询
        return this.executeSelectPagination(pagination, wrapper, WareInfoVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(WareInfoDTO dto) throws Exception {
        return this.executeSaveOrUpdate(dto) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public WareInfoVO selectById(Long wareInfoId) throws Exception {
        return this.executeSelectById(wareInfoId, WareInfoVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> wareInfoIds) {
        {
            final QueryWrapper<WareSku> wrapper = new QueryWrapper<>();
            wrapper.in(ColumnConstant.WARE_ID, wareInfoIds);
            final Integer count = this.wareSkuDao.selectCount(wrapper);
            if (count > 0) {
                throw BusinessException.instance("mall.ware.wareinfo.delete.haveSku");
            }
        }
        return this.baseMapper.deleteBatchIds(wareInfoIds) > 0;
    }

}
